package com.fh.job;


import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fh.service.cuijimanage.CuijimanageServiceImpl;
import com.fh.util.EhcacheUtil;
import com.fh.util.mail.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.*;

public class PackActionJob {
    private Logger logger = LoggerFactory.getLogger(PackActionJob.class);

    @Autowired
    private CuijimanageServiceImpl cuijimanageServiceImpl;
	protected void execute() {

        System.out.println("短信扫描30秒执行一次。。。。。。。。。。。。");
        EhcacheUtil ehCache = EhcacheUtil.getInstance();
            Object obj = ehCache.get("message", "dingshi");
            if (obj != null) {
               String str =  (String)obj;
                List<Map<String,String>>  messageList = ( List<Map<String,String>>)JSONUtils.parse(str);
                List<Map<String,String>>  messageList1 = new ArrayList<>();
                messageList1.addAll(messageList);
                Date nowDate = new Date();
                String nowDateStr = DateUtil.formatDate(nowDate,"yyyy-MM-dd HH:mm");
                for (Map<String, String> map:messageList){
                    logger.info("发送到手机号==>{}",map.get("phone_num"));
                    logger.info("短信内容==>{}",map.get("text"));
                    logger.info("发送时间==>{}",map.get("sendtime"));
                    logger.info("是否发送==>{}",map.get("status"));
                    String phone = map.get("phone_num");
                    String messagetext = map.get("text");

                    logger.info("当前时间==>{}",nowDateStr);
                    if(map.get("sendtime").equals(nowDateStr)){
                        logger.info("发送短信");
                        Map<String,String> params = new HashMap<>();
                        params.put("phone_num",phone);
                        params.put("text", messagetext);
                        String res =  cuijimanageServiceImpl.sendMessage(params);
                        logger.info("发送短信回执：{}",res);
                        if(!StringUtils.isEmpty(res)) {
                            JSONObject map2 = JSON.parseObject(res);
                            if (map2.get("code").equals("200")) {
                                cuijimanageServiceImpl.updateCallLoanConverseInfo(map.get("id"));
                            }
                        }
                        messageList1.remove(map);
                    }
                }
                String str1 = JSONUtils.toJSONString(messageList1);
                ehCache.remove("message","dingshi");
                ehCache.put("message", "dingshi", str1);
            }


    }
}
